package cz.muni.fi.pb138.bookinfo;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Availability extends ListActivity {
	private TextView selection;
	private List<String> lib = new ArrayList<String>();
	private List<String> noLibraries = new ArrayList<String>();
	private boolean libraries = false;
	
	/** Called when the activity is first created. 
	 *	Shows the libraries with the selected book.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	   super.onCreate(savedInstanceState);
	   setContentView(R.layout.availability);
	   
	   selection = (TextView)findViewById(R.id.selection);
	   Bundle data = getIntent().getExtras();
	   lib.addAll(data.getStringArrayList("list"));
	   
	   if(lib != null && lib.size() != 0){
		   selection.setText("Number of libraries found: "+ lib.size());
		   libraries = true;
		   setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lib));
	   } else {
		   libraries = false;
		   if(noLibraries.size() == 0) {
			   noLibraries.add("No libraries found!"); 
		   }	   
		   setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, noLibraries));
	   }
	   
	}
	
	@Override
	public void onListItemClick(ListView parent, View v, int position, long id){
		if(libraries == true) {
			selection.setText(lib.get(position));
		}
	}
}
